﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.EntityFramework;
using Abp.ObjectMapping;
using Abp.UI;
using AVIS.OutBound.EntityFramework.Models;
using AVIS.OutBound.Members;

namespace AVIS.OutBound.EntityFramework.Repositories
{
    public class MemberRepository :OutBoundRepositoryBase<AvisMember>, IMemberRepository
    {
        private readonly IRepository<AvisMember> _avisMemberRepository;
        private readonly IObjectMapper _objectMapper;

        public MemberRepository(IDbContextProvider<OutBoundDbContext> dbContextProvider,
            IRepository<AvisMember> avisMemberRepository,
            IObjectMapper objectMapper)
            : base(dbContextProvider)
        {
            _avisMemberRepository = avisMemberRepository;
            _objectMapper = objectMapper;
        }

        public void ModifyPassword(Member member) {
        }

        public Member GetById(int id)
        {
            AvisMember avisMember = _avisMemberRepository.FirstOrDefault(id);
            return _objectMapper.Map<Member>(avisMember);
        }

        public void Update(Member member)
        {
            AvisMember m = _avisMemberRepository.FirstOrDefault(member.ID);
            if (m != null)
            {
                m.MemberName = member.MemberName;
                m.Surname = member.Surname;
                m.Firstname = member.Firstname;
                m.Birthday = member.Birthday;
                m.Gender = member.Gender;
                m.CreditCardCode = member.CreditCardCode;
                m.CreditCardDate = member.CreditCardDate;
                m.DrivingLicences = member.DrivingLicences;
                m.ContactAddress = member.ContactAddress;
                m.Email = member.Email;
                _avisMemberRepository.Update(m);
            }
            else {
                throw new UserFriendlyException("对不起，修改失败！");
            }
        }
    }
}
